package com.ssh.service;

import java.util.List;
import java.util.Map;

import com.ssh.dao.IUsersDao;
import com.ssh.model.Users;

public class UsersService implements IUsersService {
	private IUsersDao usersDAO = null;
	
	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#addUsers(com.ssh.model.Users)
	 */
	@Override
	public void addUsers(Users u) {
		usersDAO.save(u);
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#delUsers(com.ssh.model.Users)
	 */
	@Override
	public void delUsers(Users u) {
		usersDAO.delete(u);
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#findAll()
	 */
	@Override
	public List<Users> findAll() {
		return usersDAO.findAll();
	}
	
	public List<Users> pageQuery(int start,int end)
	{
		return usersDAO.pageQuery(start, end);
	}
	
	public String count(){
		return usersDAO.count();
	}

	
	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#findById(java.lang.Object)
	 */
	@Override
	public List<Users> findById(Object id) {
		return usersDAO.findById(id);
	}

	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#updateUsers(com.ssh.model.Users)
	 */
	@Override
	public void updateUsers(Users u) {
		usersDAO.attachDirty(u);

	}
	
	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#register(java.lang.String)
	 */
	public String register(String username,String password) {
		List<Users> list = usersDAO.findByUsername(username);
		if (list.size()==0) {
			Users u=new Users();
			u.setUsername(username);
			u.setPassword(password);
			usersDAO.save(u);
			return "success";
		}else
		{
			return "failed";
		}
	}
	
	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#login(java.lang.String, java.lang.String)
	 */
	@Override
	public Users login(String username, String password) {
		List<Users> list = usersDAO.findByUsername(username);
		Users u=new Users();
		if (list.size()==0) {
			return null;
		}else{
			u=list.get(0);
			if (username.equals(u.getUsername()) && password.equals(u.getPassword())) {
				return u;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#getUsersDAO()
	 */
	@Override
	public IUsersDao getUsersDAO() {
		return usersDAO;
	}

	/* (non-Javadoc)
	 * @see com.ssh.service.IUsersService#setUsersDAO(com.ssh.dao.IUsersDao)
	 */
	@Override
	public void setUsersDAO(IUsersDao usersDAO) {
		this.usersDAO = usersDAO;
	}

}
